package com.hu.biz.signature;

import com.hu.biz.encrypt.EncryptFactory;
import com.hu.biz.encrypt.EncryptProcessor;
import com.hu.biz.encrypt.EncryptProperties;
import com.hu.common.util.adm.SpringKit;
import lombok.Getter;
import lombok.Setter;


import java.util.Map;

/**
 * @author 00351634
 * @version 1.0
 * @date 2024/10/15 8:05
 * @description: 签名处理器
 */
public abstract class AbstractSignatureHandler {
    /** 签名场景key，使用该key从配置文件中获取对应的加密算法和密钥 */
    protected String key;
    /** 加解密处理器 */
    @Getter
    @Setter
    private EncryptProcessor encryptProcessor;

    public AbstractSignatureHandler(String key) {
        this.key = key;
        EncryptProperties properties = SpringKit.getBean(EncryptProperties.class);
        Map<String, EncryptProperties.EncryptConfig> encryptConfigMap = properties.getConfigMap();
        EncryptProperties.EncryptConfig encryptConfig = encryptConfigMap.get(key);
        this.encryptProcessor = EncryptFactory.getBean(encryptConfig.getAlgorithm());
        encryptProcessor.initAlgorithm(encryptConfig.getSecret());
    }

    /**
     * 根据明文计算摘要
     *
     * @return
     */
    public abstract String calcSignature();

    /**
     * 根据摘要进行Base64(加密(摘要))
     *
     * @return
     */
    public abstract String encryptSignature();

    /**
     * 获取客户端签名解密后的摘要
     *
     * @return
     */
    public abstract String getClientSignature();

    /**
     * 验证签名
     */
    public Boolean valid() {
        return calcSignature().equals(getClientSignature());
    }
}
